//
//  HotViewController.m
//  Find
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 Reiko. All rights reserved.
//

#import "HotViewController.h"
#import "HotViewCell.h"
#import "HotViewCell2.h"
#import "LostDetailViewController.h"


@interface HotViewController ()
@property (nonatomic, retain)NSMutableArray *dataFindArray;
@property (nonatomic, retain)NSMutableArray *dataLostArray;
@end

@implementation HotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addSubviews];
    self.photoArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    AVQuery *query = [AVQuery queryWithClassName:@"CycleImage"];
//    query.cachePolicy = kAVCachePolicyCacheElseNetwork;
//    query.maxCacheAge = 60 * 60;
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        
        for (AVObject *object  in objects) {
            [self.photoArray addObject:[[object valueForKey:@"image"] url]];
            [self.titleArray addObject:[object valueForKey:@"imageTitle"]];
        }
        // 设置图片
         self.cycleSC = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, kHotCycleHeight) imageURLStringsGroup:self.photoArray];
        // 设置标题
        self.cycleSC.titlesGroup = self.titleArray;
    }];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"iconfont-navbar-back"] forBarMetrics:UIBarMetricsDefault];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 60)];
    label.text = @"热门";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"Helvetica" size:18];
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpData];
    
}

- (void)setUpData
{
    
    AVQuery *query1 = [FindMessageModel query];
    [query1 orderByDescending:@"collectCount"];
//    query1.cachePolicy = kAVCachePolicyCacheElseNetwork;
//    query1.maxCacheAge = 60 * 60;
    [query1 setLimit:6];
    [query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.dataFindArray = [NSMutableArray arrayWithArray:objects];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
    
    AVQuery *query2 = [LostMessageModel query];
//    query2.cachePolicy = kAVCachePolicyCacheElseNetwork;
//    query2.maxCacheAge = 60 * 60;
    [query2 orderByDescending:@"collectCount"];
    [query2 setLimit:6];
    [query2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.dataLostArray = [NSMutableArray arrayWithArray:objects];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
}




#pragma mark -- 布局视图
- (void)addSubviews{
    // 设置collection
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = kScreenWidth*0.04;
    layout.minimumLineSpacing = kScreenWidth*0.04;
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, kHotCycleHeight);
    layout.sectionInset = UIEdgeInsetsMake(kScreenWidth*0.04, kScreenWidth*0.04, kScreenWidth*0.04, kScreenWidth*0.04);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kTabBarHeight - kNavBarHeight) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    // 注册cell1
    [self.collectionView registerNib:[UINib nibWithNibName:@"HotViewCell" bundle:nil] forCellWithReuseIdentifier:@"HotViewCell"];
    // 注册cell2
    [self.collectionView registerNib:[UINib nibWithNibName:@"HotViewCell2" bundle:nil] forCellWithReuseIdentifier:@"HotViewCell2"];
    // 注册表头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HotHeader"];
    
    
}

#pragma mark -- collectionView代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataFindArray.count + self.dataLostArray.count;
    
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row <= self.dataFindArray.count - 1) {
        HotViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotViewCell" forIndexPath:indexPath];
        FindMessageModel *model = self.dataFindArray[indexPath.row];
        NSString *url = [model.imageArray firstObject];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder2"]];
        cell.collectCountLabel.text = [NSString stringWithFormat:@"(%@)",model.collectCount];
        cell.titleLable.text = model.title;
        
        
        return cell;
    } else {
        
        HotViewCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotViewCell2" forIndexPath:indexPath];
        LostMessageModel *model = self.dataLostArray[indexPath.row - 6];
        NSString *url = [model.imageArray firstObject];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder2"]];
        cell.collectCountLabel.text = [NSString stringWithFormat:@"(%@)",model.collectCount];
        cell.titleLable.text = model.title;
        cell.backgroundColor = [UIColor colorWithRed:0.341 green:0.749 blue:1.000 alpha:0.3];
        return cell;
    }
}
// 设置表头表尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HotHeader" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor greenColor];
        [self addCycleSC];
        [headerView addSubview:self.cycleSC];
        return headerView;
    } else {
        return nil;
    }
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row <= self.dataFindArray.count - 1) {
        return CGSizeMake(kScreenWidth*0.43, 230);
    } else {
        return CGSizeMake(kScreenWidth*0.92, 111);
    }
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row <= self.dataFindArray.count - 1) {
        FindMessageModel *model = self.dataFindArray[indexPath.row];
        FindDetailViewController *findVC = [[FindDetailViewController alloc] init];
        findVC.objectID = model.objectId;
        
        // 关闭点击方法
        [collectionView setUserInteractionEnabled:NO];
        [self.navigationController pushViewController:findVC animated:YES];
    } else {

        LostMessageModel *model = self.dataLostArray[indexPath.row - 6];
        LostDetailViewController *lostVC = [[LostDetailViewController alloc] init];
        lostVC.objectID = model.objectId;
        
        // 关闭点击方法
        [collectionView setUserInteractionEnabled:NO];
        [self.navigationController pushViewController:lostVC animated:YES];
        
    }
}
#pragma mark -- 添加轮播图
- (void)addCycleSC{
   
    self.cycleSC.backgroundColor = [UIColor lightGrayColor];
   
    // 标题背景颜色
    self.cycleSC.titleLabelBackgroundColor = [UIColor whiteColor];
    // 标题背景高度
    self.cycleSC.titleLabelHeight = 200 * 0.1;
    // 字体属性
    self.cycleSC.titleLabelTextColor = [UIColor blackColor];
    self.cycleSC.titleLabelTextFont = [UIFont boldSystemFontOfSize:kHotCycleLabelFont];
    // 图片间隔
    self.cycleSC.autoScrollTimeInterval = 1.5;
    // 小圆点的属性
    self.cycleSC.dotColor = [UIColor redColor];
    self.cycleSC.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    self.cycleSC.pageControlDotSize = CGSizeMake(5, 5);
    self.cycleSC.delegate = self;
    self.cycleSC.placeholderImage = [UIImage imageNamed:@"placeholder2"];
    
}

#pragma mark --- 将要出现的时候打开 collectionView 交互功能
- (void)viewDidAppear:(BOOL)animated
{
    [self.collectionView setUserInteractionEnabled:YES];
}

#pragma mark -- 轮播图唯一的代理方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{

    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
